/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.gadgets.phoneBook.client;

import java.util.ArrayList;

import com.gadglet.client.gwt.GadgetUtils;
import com.gadglet.client.gwt.ui.DebugDialogBox;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.PreferencesFeature;
import com.google.gwt.gadgets.client.PreferencesProvider;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ResultsGrid {

	private Grid results = null;
	private int lastItem = 0;
	private int tabSelected =0;
	private int maxRows =10;
	int lastStart = 0;

	private Label nextLink=null; 
	private Label prevLink=null;

	private DebugDialogBox errorNotifier = DebugDialogBox.getErrorNotifier();

	ResultsGrid(int tabSelected){
		this.tabSelected = tabSelected;
		results = new Grid(1,2);
		PreferencesFeature  prefUtil = PreferencesProvider.get();
		nextLink = new Label(prefUtil.getMsg("gadgetLabelNext"));
		nextLink.setStyleName("nextPrev");
		nextLink.setVisible( false);
		nextLink.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				results.resize(1, 2);
				changeData(lastItem);
				 
			}
		});


		prevLink = new Label(prefUtil.getMsg("gadgetLabelPrevious"));
		prevLink.setVisible( false);
		prevLink.setStyleName("nextPrev");
		prevLink.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				results.resize(1, 2);
				changeData(lastStart-maxRows);
				
				
			}
		});

	}

	void changeData(int StartFrom){
		if(GoogleContacts.contactsList.size()==0)
			GoogleContacts.callFeed();
			
		lastStart = StartFrom;
		
		String rowStyle = "";
		int validCounter = 0;
		int contactsCounter = 0;
	
		String formatedName = null;
		Contact currentContact =null;
		ArrayList<PhoneNumber> nums = null;
		HorizontalPanel prevnextPanel = new HorizontalPanel();
		
		PreferencesFeature  prefUtil = PreferencesProvider.get();

		results.setWidth("100%");
		results.setBorderWidth(0);
		results.getColumnFormatter().setWidth(0, "50%");

		results.setWidget(0, 0, new HTML("<b>"+PhoneBookPanel.tabLabels[tabSelected]+"</b>"));
		prevnextPanel.add(prevLink);
		prevnextPanel.add(new HTML("&nbsp;&nbsp;&nbsp;"));
		prevnextPanel.add(nextLink);
		results.setWidget(0, 1, prevnextPanel);
		
		nextLink.setVisible(false);
		prevLink.setVisible(false);
		
		
		while ( contactsCounter<GoogleContacts.contactsList.size())
		{

			 currentContact = new Contact(GoogleContacts.contactsList.get(contactsCounter));
			formatedName = currentContact.getName();
			 
			
			// find names srarts with "?"
			if(formatedName!=null && formatedName.substring(0, 1).compareToIgnoreCase(PhoneBookPanel.tabLabels[tabSelected])==0)
			{	
				validCounter++;
			
				// skip prev items
				if(validCounter<=StartFrom)
				{
					contactsCounter++;
					prevLink.setVisible(true);
					continue;
				}
					// exit when exceed maxRows
				if(validCounter>(StartFrom+maxRows))
				{
					nextLink.setVisible(true);
					break;
				}
				
				
				lastItem = validCounter;
			
				nums = currentContact.getPhoneNumbers();
				
				results.resizeRows(results.getRowCount()+1);

				if (((validCounter-StartFrom)%2)==1)
					rowStyle = "gridRowShadow";
				else
					rowStyle = "gridRowNoShadow";
					
				results.getRowFormatter().setStyleName(validCounter-StartFrom, rowStyle);

				results.setText(validCounter-StartFrom, 0, formatedName);
				
				// check for valid numbers
				if (nums!=null && nums.size()>0)
				{
					VerticalPanel vp = new VerticalPanel();
					// loop on numbers
					for (int num = 0 ; num < nums.size();num++ )
					{

						HorizontalPanel hp = new HorizontalPanel();
						Image icon = new Image();
						icon.setUrl(GadgetUtils.getGadgetIconURL(PhoneBook.gadgetName, nums.get(num).getNumberType(), null, false));

						String iconTitle ="";
						try
						{
							iconTitle = prefUtil.getMsg("gadgetLabel"+nums.get(num).getNumberType());

						}
						catch (Exception e)
						{
							if (PhoneBook.debug)
								errorNotifier.showError(1, e.getMessage());
						}
						icon.setTitle(iconTitle);
						Label n = new Label(nums.get(num).getNumber());
						n.setTitle(prefUtil.getMsg("gadgetLabel"+nums.get(num).getNumberType()));
						hp.add(n);
						//hp.add(new HTML(nums.get(num).getNumber()));
						hp.setBorderWidth(0);
						hp.add(icon);

						vp.add(hp);

					}
					results.setWidget(validCounter-StartFrom, 1,vp);

				}
			}
		
			contactsCounter++;
		}
	
		
		PhoneBookPanel.data.add(results);
	}
}
